var scrollTopArr = new Array();
var pageSlideHold = 400;
var isNeedBackPage = true;

window.onload = function(){
	$("iframe.lazy-frame").each(function(){
		var $iframe = $(this);
		var lazySrc = $iframe.attr("lazy-src");
	    if(lazySrc && lazySrc != ""){
	    	$iframe[0].onload = function(){
	    		this.style.opacity = "1";
	    	};
	    	$iframe[0].src = lazySrc;
	    	$iframe.removeAttr("lazy-src");
	    }
	});
};

var last_pageIdOrUrl;
var last_param;
var last_effect;
var stFrameLevel = 1;
var last_speedTime;

var _BrowserWindow;
function openHrmBrowser(theWindow, fieldId, fieldSpanId, isMuti){
	var browserType = (isMuti == true || isMuti == "1") ? "1" : "2";
	var paramstr = "fieldId="+(fieldId || "")+"&fieldSpanId="+(fieldSpanId || "")+"&browserType="+browserType;
	openHrmBrowser2(theWindow, paramstr);
}

function openHrmBrowser2(theWindow, paramstr){
	_BrowserWindow = theWindow;
	
	var defaultParamCfg = {
		"fieldId" : "",			//字段id
		"fieldSpanId" : "",		//字段显示区域id
		"browserType" : "1",	//类型：1.多选  2.单选
		"selectedIds" : ""		//选中的id，逗号分隔，如：1,2,3
	};
	
	var containSelectedId = false;
	var paramArr = paramstr.split("&");
	for(var i = 0; i < paramArr.length; i++){
		var oneParam = paramArr[i];
		var oneParamArr = oneParam.split("=");
		var	p_name = oneParamArr[0];
		var p_value = oneParamArr[1];
		defaultParamCfg[p_name] = p_value;
		if(p_name == "selectedIds"){
			containSelectedId = true;
		}
	}
	
	if(!containSelectedId){
		var fieldId = defaultParamCfg["fieldId"];
		var theDocument = theWindow.document;
		var fieldEle = theDocument.getElementById(fieldId);
		if(fieldEle){
			defaultParamCfg["selectedIds"] = fieldEle.value;
		}
	}
	var hrmBrowserFrame = document.getElementById("hrmBrowserFrame");
	var hrmBrowserWindow = hrmBrowserFrame.contentWindow;
	
	if(hrmBrowserWindow){
		hrmBrowserWindow.resetBrowser(defaultParamCfg);
	}
}

function openCommonBrowser(theWindow, fieldId, fieldSpanId, browserId, browserName, browserText){
	_BrowserWindow = theWindow;
	var defaultParamCfg = {
		"fieldId" : fieldId || "",			//字段id
		"fieldSpanId" : fieldSpanId || "",	//字段显示区域id
		"browserId" :  browserId || "",		//浏览框类型
		"browserName" : browserName || "",	//浏览框名称
		"browserText" : browserText || ""
	};
	
	try{
		var theDocument = theWindow.document;
		var fieldEle = theDocument.getElementById(fieldId);
		if(fieldEle){
			defaultParamCfg["selectedIds"] = fieldEle.value;
		}
	}catch(ex){}
	
	var commonBrowserFrame = document.getElementById("commonBrowserFrame");
	var commonBrowserWindow = commonBrowserFrame.contentWindow;
	if(commonBrowserWindow){
		commonBrowserWindow.resetBrowser(defaultParamCfg);
	}
}

function toShowBrowser(obj,objid){
	$("#footerDiv").attr("currentField",objid);
	toShowFooterPop(3,objid);
}

function toShowBrowser_old(obj,objid){
	$("#browserType").removeClass("hide");
	$("#browserType").attr("currentField",objid);
	var oldtype = 0;
	if($("#"+objid).val()!=""){
		oldtype = 1;
	}else if($("#"+objid+"dpid").val()!=""){
		oldtype = 2;
	}else if($("#"+objid+"all").val()!=""){
		oldtype = 3;
	}
	$("#browserTypeUl li").removeClass("selected");
	if(oldtype>0){
		$("#browserTypeUl li:nth-child("+oldtype+")").addClass("selected");
	}
}

function openBrowserByType(obj){
	var type = $(obj).attr("type");
	var footerDiv = $("#footerDiv");
	var currentField = footerDiv.attr("currentField");
	if(type==1||type==2){//人力资源  //部门
		footerDiv.attr("type",type);
		if(type==1){
			openBrowser(currentField, currentField+'Span', '17', '', '');
		}else{
			openBrowser(currentField+"dpid", currentField+'Span', '57', '', '');
		}
	}else if(type==3){//所有人
		setBrowserValue(3,1,"所有人");
	}
}

function setBrowserValue(type,value,showname){
	var footerDiv = $("#footerDiv");
	var currentField = footerDiv.attr("currentField");
	$("#"+currentField).val("");
	$("#"+currentField+"dpid").val("");
	$("#"+currentField+"all").val("");
	if(type==1){
		$("#"+currentField).val(value);
	}else if(type==2){
		$("#"+currentField+"dpid").val(value);
	}else if(type==3){
		$("#"+currentField+"all").val(1);
	}
	$("#"+currentField+"Span").html(showname);
}

var isOpenBrowser = false;
var isUserBrowserOk = false;
function openBrowser(a, d, c, e, b) {
	$("#browserFrames").removeClass("hide");
	$("#detailPanel").addClass("hideLeft2");
	$("#browserFrames iframe").hide();
	if (c == "1") {
		  $("#hrmBrowserFrame").show();
          openHrmBrowser(window,a, d, false)
    }else if (c == "17") {
    	  $("#hrmBrowserFrame").show();
    	  openHrmBrowser(window,a, d, true)
    }else{
    	  $("#commonBrowserFrame").show();
    	  openCommonBrowser(window,a, d, c, e, b)
    }
	pushHistoryState({url:"c"});
	isOpenBrowser = true;
	isUserBrowserOk = false;
};

function onBrowserBack(){
	$("#browserFrames").addClass("hide");
	$("#detailPanel").removeClass("hideLeft2");
}

function onBrowserOk(result){
	var fieldId = result["fieldId"];
	var fieldSpanId = result["fieldSpanId"];
	var idValue = result["idValue"];
	var nameValue = result["nameValue"];
	
	var type = $("#footerDiv").attr("type");
	setBrowserValue(type,idValue,nameValue);
	$("#browserFrames").addClass("hide");
	$("#detailPanel").removeClass("hideLeft2");
	isUserBrowserOk = true;
	doHistoryBack();
}